package service;

import mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import po.Admin;
import po.Result;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public String findPwdByUsername(String username) throws Exception {
        return adminMapper.findPwdByUsername(username);
    }

    @Override
    public Result updatePwdByUsername(String username, String oldPwd, String newPwd) throws Exception {
        Result result = new Result();
        String pwd = adminMapper.findPwdByUsername(username);
        if (oldPwd.equals(pwd)) {
            try {
                Admin admin = new Admin();
                admin.setUsername(username);
                admin.setPassword(newPwd);
                adminMapper.updatePwdByUsername(admin);
            } catch (Exception e) {
                result.setResult("fail");
                result.setMessage("修改失败！");
                return result;
            }
            result.setResult("success");
            result.setMessage("修改成功！");
            return result;
        }
        result.setResult("fail");
        result.setMessage("旧密码错误！");
        return result;
    }
}
